﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ShareFolder.ViewModel;

namespace ShareFolder
{
    public partial class EditConnection : PhoneApplicationPage
    {
        private BackgroundWorker _bw;

        private aa _smb;

        public EditConnection()
        {
            this.InitializeComponent();
            if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] != Visibility.Visible)
            {
                this.LayoutRoot.Background = null;
            }
        }

        private void _smb_ConnectedCompleted(object sender, EventArgs e)
        {
            if (this._smb.p())
            {
                this._smb.h(new EventHandler<ay>(this._smb_EnumSharesCompleted));
                this._smb.i();
                return;
            }
            if (!this._smb.l())
            {
                MessageBox.Show(this._smb.m());
                this.ContentPanel.Visibility = Visibility.Visible;
                this.ContentResources.Visibility = Visibility.Collapsed;
                base.ApplicationBar.IsVisible = true;
            }
        }

        private void _smb_EnumSharesCompleted(object object_0, ay ay_0)
        {
            if (this._smb.m() == null)
            {
                this.lstBoxResources.ItemsSource = ay_0.a;
                this.txtStatus.Visibility = Visibility.Collapsed;
                return;
            }
            if (!this._smb.l())
            {
                MessageBox.Show(this._smb.m());
            }
        }

        private void btnChoose_Click(object sender, RoutedEventArgs e)
        {
            if (!this.method_1())
            {
                return;
            }
            this.lstBoxResources.ItemsSource = null;
            this.ContentPanel.Visibility = Visibility.Collapsed;
            this.ContentResources.Visibility = Visibility.Visible;
            this.txtStatus.Visibility = Visibility.Visible;
            base.ApplicationBar.IsVisible = false;
            this._smb = new aa();
            this._smb.h(new EventHandler(this._smb_ConnectedCompleted));
            this._smb.i(this.txtHost.Text, this.txtUser.Text, this.txtPassword.Password);
        }

        private void lstBoxResources_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.lstBoxResources.SelectedItem != null)
            {
                this.txtSharedFolder.Text = this.lstBoxResources.SelectedItem.ToString();
                this.ContentPanel.Visibility = Visibility.Visible;
                this.ContentResources.Visibility = Visibility.Collapsed;
                base.ApplicationBar.IsVisible = true;
            }
        }

        

        private bool method_1()
        {
            if (this.txtName.Text == "")
            {
                MessageBox.Show("You must specify a connection name!");
                return false;
            }
            if (this.txtHost.Text == "")
            {
                MessageBox.Show("You must specify a valid host!");
                return false;
            }
            if (App.ViewModel.SelectedConnection == null || this.txtName.Text.ToUpper().Trim() != App.ViewModel.SelectedConnection.Name.ToUpper().Trim())
            {
                if (App.ViewModel.Items.Any<ConnectionViewModel>((ConnectionViewModel connectionViewModel_0) => connectionViewModel_0.Name.ToUpper().Trim() == this.txtName.Text.ToUpper().Trim()))
                {
                    MessageBox.Show("The connection name already exists!");
                    return false;
                }
            }
            return true;
        }

        private bool method_2()
        {
            ConnectionViewModel text;
            if (this.txtSharedFolder.Text == "")
            {
                MessageBox.Show("You must specify a valid shared folder!");
                return false;
            }
            if (!this.method_1())
            {
                return false;
            }
            text = (App.ViewModel.SelectedConnection != null ? App.ViewModel.SelectedConnection : new ConnectionViewModel());
            text.Name = this.txtName.Text;
            text.Host = this.txtHost.Text;
            text.Service = this.txtSharedFolder.Text;
            text.User = this.txtUser.Text;
            text.Password = this.txtPassword.Password;
            if (App.ViewModel.SelectedConnection == null)
            {
                App.ViewModel.AddConnection(text);
            }
            App.ViewModel.SaveData();
            return true;
        }

        private void mnuDelete_Click(object sender, EventArgs e)
        {
            if (App.ViewModel.SelectedConnection == null)
            {
                base.NavigationService.GoBack();
            }
            else if (MessageBox.Show("Are you sure?", "Delete connection", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                App.ViewModel.Items.Remove(App.ViewModel.SelectedConnection);
                App.ViewModel.SaveData();
                base.NavigationService.GoBack();
                return;
            }
        }

        private void mnuSave_Click(object sender, EventArgs e)
        {
            if (this.method_2())
            {
                base.NavigationService.GoBack();
            }
        }

        private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
        {
            if (this.ContentResources.Visibility == Visibility.Visible)
            {
                this.ContentPanel.Visibility = Visibility.Visible;
                this.ContentResources.Visibility = Visibility.Collapsed;
                base.ApplicationBar.IsVisible = true;
                this._smb.k();
                e.Cancel = true;
            }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.ViewModel.SelectedConnection != null)
            {
                this.txtName.Text = App.ViewModel.SelectedConnection.Name;
                this.txtHost.Text = App.ViewModel.SelectedConnection.Host;
                this.txtSharedFolder.Text = App.ViewModel.SelectedConnection.Service;
                if (App.ViewModel.SelectedConnection.User != null)
                {
                    this.txtUser.Text = App.ViewModel.SelectedConnection.User;
                }
                this.txtPassword.Password = App.ViewModel.SelectedConnection.Password;
            }
        }
    }
}